package com.slmxz.tms.common.support;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.slmxz.tms.common.model.Query;
import org.springframework.beans.BeanUtils;

import java.util.Map;

/**
 * @Author：albert
 * @Date：2024/9/27 22:32
 */
public class Condition {
    public static <T> IPage<T> getPage(Query query) {
        Page<T> page = new Page<>(query.getPageNum(),query.getPageSize());
        page.setOrders(OrderItem.ascs(StrUtil.splitToArray(SqlKeyword.filter(query.getAscs()),",")));
        page.setOrders(OrderItem.descs(StrUtil.splitToArray(SqlKeyword.filter(query.getDescs()),",")));
        return page;
    }

    public static <T> QueryWrapper<T> getQueryWrapper(T entity) {
        return new QueryWrapper<>(entity);
    }

    public static <T> QueryWrapper<T> getQueryWrapper(Map<String, Object> query, Class<T> clazz) {
        query.remove("pageNum");
        query.remove("pageSize");
        QueryWrapper<T> qw = new QueryWrapper<>();
        qw.setEntity(BeanUtils.instantiateClass(clazz));
        SqlKeyword.buildCondition(query, qw);
        return qw;
    }


}
